package com.illtamer.infinite.collapsar.common;

import lombok.SneakyThrows;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecurityUtil {

    @SneakyThrows(NoSuchAlgorithmException.class)
    public static String md5Encode(String s) {
        byte[] secretBytes = MessageDigest.getInstance("md5").digest(s.getBytes(StandardCharsets.UTF_8));
        StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));// 16进制数字
        // 如果生成数字未满32位，需要前面补0
        for (int i = 0; i < 32 - md5code.length(); ++ i) {
            md5code.insert(0, '0');
        }
        return md5code.toString();
    }

}
